<?php

/**
 * This is the model class for table "tbl_jobs_status".
 *
 * The followings are the available columns in table 'tbl_jobs_status':
 * @property integer $id
 * @property string $description
 * @property integer $inactive
 * @property integer $sort
 */
class JobsStatus extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return JobsStatus the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_jobs_status';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('description, inactive', 'required'),
			array('inactive, sort', 'numerical', 'integerOnly'=>true),
			array('description', 'length', 'max'=>36),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, description, inactive, sort', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'description' => 'Description',
			'inactive' => 'Inactive',
			'sort' => 'Sort',
		);
	}

        public static function getListData(){
			$criteria = new CDbCriteria;
			$criteria->order = 'sort ASC';
			$criteria->condition = 'inactive=0';
            $list=JobsStatus::model()->findAll($criteria);
            $rs=array();
            if(count($list)>0){
                foreach($list as $item){
                    $rs[$item->id]=$item->description;
                }
            }
            return $rs;
        }
        public static function getText($id){
            $data=Yii::app()->db->createCommand('select description from tbl_jobs_status where id=:id')->bindValue('id', $id)->queryScalar();
            if($data!==false){
                return $data;
            }
            return NULL;
        }
        public static function getId($text){
            $data=Yii::app()->db->createCommand('select id from tbl_jobs_status where description=:text')->bindValue('text', $text)->queryScalar();
            if($data!==false){
                return $data;
            }
            return NULL;
        }
       public static function getJobStatusListAndCount($empno=false){
            $list=self::getListData();                      
            $rs=array();
            
         
            foreach($list as $id=>$status){
                if($empno)
                    $count=Yii::app()->db->createCommand('select count(*)
                                                         from tbl_jobs
                                                         where jobstatus=:status and jobsalesperson=:empno')
                        ->bindValues(array('status'=> $id,'empno'=>$empno))->queryScalar();
                    else
                $count=Yii::app()->db->createCommand('select count(*) from tbl_jobs where jobstatus=:status')
                        ->bindValue('status', $id)->queryScalar();
                if($count!==false && $count!=null){
                    $rs[$status]=$count;
                }else{
                    $rs[$status]=0;
                }
            }
        
            return $rs;
        }
}
